package cn.com.huzo.webapp.controller.master;

import cn.com.huzo.model.entities.master.WareHouseEntity;
import cn.com.huzo.service.master.WareHouseLocationManager;
import cn.com.huzo.service.master.WareHouseManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 仓库画面控制器
 *
 * @author 吴旭
 */
@Controller
public class WareHouseController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">   
    @Resource(name = "WareHouseManager")
    private WareHouseManager _manager_warehouse;

    @Resource(name = "WareHouseLocationManager")
    private WareHouseLocationManager _manager_warehouse_location;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到仓库一览画面
     *
     * @return
     */
    @RequestMapping("master/warehouse/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.warehouse.list.def");
        return view;
    }

    /**
     * 仓库编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param wareHouseId 仓库标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse/{action}/{wareHouseId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "wareHouseId") String wareHouseId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("wareHouseId", wareHouseId);

        view.setViewName("master.warehouse.edit.def");
        return view;
    }

    /**
     * 弹出仓库选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/warehouse/choice.html")
    public ModelAndView redirectChoicePopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.warehouse.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="仓库数据处理部分">
    /**
     * 根据标识取得数据。
     *
     * @param criterion 仓库标识
     * @return 仓库数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_warehouse.fetchByHeaderKey(criterion);
    }

    /**
     * 得到全部仓库分页数据。
     *
     * @param criterion 检索条件
     * @return 仓库分页数据
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "master/warehouse/fetchForPage.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_warehouse.fetchForPage(criterion);
    }

    /**
     * 增加仓库。
     *
     * @param instance 要新增的数据
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final WareHouseEntity instance) throws Exception {
        if (instance.getWareHouseId() != 0) {
            return _manager_warehouse.update(instance).longValue();
        } else {
            return _manager_warehouse.insert(instance);
        }
    }

    /**
     * 删除仓库。
     *
     * @param instance 要删除的数据
     * <p>
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> instance) throws Exception {
        return _manager_warehouse.delete(instance);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="库位数据处理部分">
    /**
     * 批量增加库位。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse_location/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsert(@RequestBody final Map<String, Object> itemList) throws Exception {
        return _manager_warehouse_location.batchInsert(itemList);
    }

    /**
     * 删除库位。
     *
     * @param instance 要删除的成员
     * @return 库位和翻页数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse_location/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> instance) throws Exception {
        return _manager_warehouse_location.deleteLines(instance);
    }

    /**
     * 批量删除库位。
     *
     * @param itemList 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse_location/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDelete(@RequestBody final Map<String, Object> itemList) throws Exception {
        return _manager_warehouse_location.batchDelete(itemList);
    }

    /**
     * 根据仓库标识取得库位。
     *
     * @param criterion 仓库标识,翻页数据等
     * @return 库位，翻页数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/warehouse_location/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchLocationForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_warehouse_location.fetchForPage(criterion);
    }
    // </editor-fold>
}
